<?php

/*
Plugin Name: Products Custom Posts
Plugin URI:
Description: Plugin to create custom post type of Products for Soot Seekers.
Version: 1.0
Author: Kyle Finley
Author URI: http://www.page110.com/
License: GPL2
*/

// Defines
 
define( 'PRODUCTS_SETTINGS', 'products_settings' );
define( 'PRODUCTS_META_KEY', 'products_details' );
 
// Includes

require_once ( 'include/products.php' );
require_once ( 'include/products-template-tags.php' );

 
// On plug-in activation
 
register_activation_hook( __FILE__, 'products_activate'); 
 
// Wordpress hooks

add_action( 'init','products_init' );

add_action( 'add_meta_boxes', 'products_add_metabox', 10, 2 );
add_action( 'save_post', 'soot_seekers_products_on_post_save', 10, 2 );
add_action( 'wp_enqueue_scripts', 'add_my_stylesheet' );
 
// callback function for template redirect to add in CSS 
function add_my_stylesheet() {
	wp_enqueue_style('products-css', plugins_url('css/products.css', __FILE__), array(), '1.0');
}

//callback function run on plugin activation 
function products_activate() {
	 
	if( version_compare( get_bloginfo( 'version' ), '3.0', '<') ) {
		 
		deactivate_plugins( __FILE__ );
		
	} else {
	
		flush_rewrite_rules();
		
	}
 
}

// callback function for init hook
function products_init() {
	
	// register the portfolio custom post type
	soot_seekers_register_products();
		
}

// Create custom meta boxes

function products_add_metabox( $post_type, $post ) {
	
	add_meta_box( 'products_meta',			// CSS ID of the <div> that wraps the metabox
                      'Soot Seekers Product',		// title of the meta box
                      'products_display_metabox',	// callback for rendering metabox content
                      'product',			// post type for metabox display (could also be 'post' or 'custom post type')
                      'normal',				// context - part of the page where the meta will show
                      'high'				// priority of metabox display related to other metaboxes on the page
                      );
	
	// display portfolio metabox	  
	function products_display_metabox( $post ) {
		
		// fetch post-meta data for this post, if it exists
		$products_input = get_post_meta( $post->ID, PRODUCTS_META_KEY, true ); 
		
		// if not, initialize with defaults
		if( $products_input == false ) {
			
			$products_input = array();
			//$products_input['title'] = 'Same as Title at the top.';
			$products_input['image'] = 'Copy Featured Image URL';
			$products_input['description'] = 'Enter a short description of the product.';
			$products_input['price'] = '';
                        $products_input['url'] = 'http://';
			
		}
		
		// convert portfolio input array to variables
		extract( $products_input );
	
		// display the form
		include 'interface/products-metabox.php';
		
	}
	
}

// run when a post is saved
function soot_seekers_products_on_post_save( $post_id, $post ) {

	// bail on autosave
	if( $post->post_type == 'revision' ) {
	
		return;
		
	}
	
	if( isset( $_POST['products'] ) ) {
		
		$products_input = $_POST['products'];
		
		$products_input['description'] = strip_tags( $products_input['description'] );
		$products_input['url'] = esc_url( $products_input['url'] );
                
		update_post_meta( $post_id, PRODUCTS_META_KEY, $products_input );
			
	}
	
}

?>